﻿#include "screenscutcheonhandler.h"

using namespace EarthView::World::Spatial::SystemUI;
using namespace EarthView::World::Spatial3D::Controls;
using namespace EarthView::World::Spatial::Math;
using namespace EarthView::World::Core;



            CScreenScutcheonHandler* CScreenScutcheonHandler::mpSingleton = NULL;

            ev_bool CScreenScutcheonHandler::mIsHandlerAdded = false ;

            CScreenScutcheonHandler::CScreenScutcheonHandler()
            {
                this->ref_mpGlobeControl=NULL;
                this->mpLastGuiEvent=NULL;
                this->mPos=CVector2::ZERO;
                this->mPanelElementName="";
            }

            CScreenScutcheonHandler::~CScreenScutcheonHandler()
            {
                ref_mpGlobeControl->removeHandler(this);
                //mpScreenScutcheonList.clear();
                this->ref_mpGlobeControl = NULL;
                this->mIsHandlerAdded=false;
            }

            CScreenScutcheonHandler& CScreenScutcheonHandler::getSingleton()
            {
                if (!mpSingleton)
                {
                    mpSingleton=EV_NEW CScreenScutcheonHandler();
                }
                return *mpSingleton;
            }

            CScreenScutcheonHandler* CScreenScutcheonHandler::getSingletonPtr()
            {
                if (!mpSingleton)
                {
                    mpSingleton=EV_NEW CScreenScutcheonHandler();
                }
                return mpSingleton;
            }

            ev_bool CScreenScutcheonHandler::handleEvent(CGUIEvent* guiEvent)
            {
				//////zhangjf 修改，防止锁定相机时，不能拖动标牌
               /* if(guiEvent->getHandled())
                {
                    return false;
                }*/
                switch(guiEvent->getEventType())
                {
                case CGUIEvent::FRAME:
                    {
                        return handleFrameEvent(guiEvent);
                    }
                case CGUIEvent::MOUSEDOWN:
                    {
                        return handleMouseDownEvent(guiEvent);
                    }
                case CGUIEvent::MOUSEMOVE:
                    {
                        return handleMouseMoveEvent(guiEvent);
                    }
                case CGUIEvent::MOUSEWHEEL:
                    {
                        return handleMouseWheelEvent(guiEvent);
                    }
                case CGUIEvent::MOUSEUP:
                    {
                        return handleMouseUpEvent(guiEvent);
                    }
                case CGUIEvent::KEYDOWN:
                    {
                        return handleKeyDownEvent(guiEvent);
                    }
                case CGUIEvent::KEYUP:
                    {
                        return handleKeyUpEvent(guiEvent);
                    }
                default:
                    break;
                }
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleFrameEvent(CGUIEvent* guiEvent)
            {
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleMouseDownEvent(CGUIEvent* guiEvent)
            {
                this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                if (mScutcheonMap.size()<1) return false;
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_MouseDown,x,y);
                if (guiEvent->getButtonMask() == CGUIEvent::LEFT_MOUSE_BUTTON)
                {
                    mpLastGuiEvent=new CGUIEvent(*guiEvent);
                    this->computeSelected(x,y);
                    if (mPanelElementName !="")
                    {
                        this->mPos=mScutcheonMap[mPanelElementName]->getPanelElementPostion(mPanelElementName);
                        return true;
                    }
                }
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleMouseMoveEvent(CGUIEvent* guiEvent)
            {
               /* this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                if (mScutcheonMap.size()<1) return false;
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_MouseMove,x,y);
                if(mPanelElementName !="" && this->mScutcheonMap[mPanelElementName]->getDraggable())
                {
                    ev_int32 dx = x - mpLastGuiEvent->getX();
                    ev_int32 dy = y - mpLastGuiEvent->getY();
                    if(dx * dx + dy * dy < 5)
                    {
                        return false;
                    }
                    mScutcheonMap[mPanelElementName]->setPanelElementPostion(mPanelElementName, CVector2(mPos.x+dx, mPos.y+dy));
                    return true;
                }*/

                return false;
            }

            ev_bool CScreenScutcheonHandler::handleMouseWheelEvent(CGUIEvent* guiEvent)
            {
               /* this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_MouseWheel,x,y);*/
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleMouseUpEvent(CGUIEvent* guiEvent)
            {
                this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                if (mScutcheonMap.size()<1) return false;
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_MouseUp,x,y);
                mPanelElementName="";
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleKeyDownEvent(CGUIEvent* guiEvent)
            {
               /* this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                if (mScutcheonMap.size()<1) return false;
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_KeyDown,x,y);*/
                return false;
            }

            ev_bool CScreenScutcheonHandler::handleKeyUpEvent(CGUIEvent* guiEvent)
            {
               /* this->mScutcheonMap= CScreenScutcheonManager::getSingletonPtr()->getScutcheonMap();
                if (mScutcheonMap.size()<1) return false;
                ev_int32  x = guiEvent->getX();
                ev_int32 y = guiEvent->getY();
                this->sendEvent(CScreenScutcheonEvent::STET_KeyUp,x,y);*/
                return false;
            }

            void CScreenScutcheonHandler::setGlobeControl(CGlobeControl* ref_globeControl)
            {
                this->ref_mpGlobeControl=ref_globeControl;
                this->ref_mpGlobeControl->addGuiHandler(this);
            }

            void CScreenScutcheonHandler::computeSelected(ev_int32 x, ev_int32 y)
            {
                for (CScutcheonMap::iterator itr=mScutcheonMap.begin();itr!=mScutcheonMap.end();itr++)
                {
                    CScreenScutcheon*  scutcheon = itr->second;
					if(scutcheon->getSelectable() && scutcheon->getVisible())
					{
                        bool m=scutcheon->isInBoundingBox(x,y);
                        if (m)
                        {
                            this->mPanelElementName=scutcheon->getName();
                            CScreenScutcheonEvent evt(CScreenScutcheonEvent::STET_Selected);	
                            evt.setSelectedScreenScutcheon(scutcheon);
                            if (scutcheon->getEventObject())
                            {
                                 CEventDispatcher::sendEvent(scutcheon->getEventObject(), &evt);
                            }
                        }
					}
                }
            }

            void CScreenScutcheonHandler::sendEvent( const CScreenScutcheonEvent::EVScreenScutcheonEventType& type, const int& x, const int& y )
            {
                CScreenScutcheonEvent evt(type);
                evt.setX(x);
                evt.setY(y);
                for (CScutcheonMap::iterator itr=mScutcheonMap.begin();itr!=mScutcheonMap.end();itr++)
                {
                    CScreenScutcheon*  scutcheon = itr->second;
                    if(scutcheon)
                    {
                        if(scutcheon->getEventObject())
                        {
                            CEventDispatcher::sendEvent(scutcheon->getEventObject(), &evt);
                        }
                    }
                }
            }
